import { request } from './index.ts'
import { PageQueryDto, PageResult } from '@/api/types'

export function getCrudApi<T>(name: string) {
  return {
    get: (id: string) => request.get<T>(`/${name}/${id}`),
    all: (query?: any) => request.get<T[]>(`/${name}`, { params: query }),
    page: (query: PageQueryDto<T>) => request.post<PageResult<T>>(`/${name}/page`, query),
    delete: (id: string) => request.delete(`/${name}/${id}`),
    update: (id: string, form: T) => request.put(`/${name}/${id}`, form),
    patch: (id: string, form: T) => request.patch(`/${name}/${id}`, form),
    create: (form: T) => request.post(`/${name}`, form),
  }
}
